//
//  ProView.swift
//  OnemeetingDemo
//
//  Created by zhangheng on 2020/2/7.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class ProView: MSView {
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func height(t:String,pt:String) -> CGFloat{
        let tw = WHString.shared.getNormalStrW(str: t, strFont: 12, h: 16)
        let ptw = WHString.shared.getNormalStrW(str: pt, strFont: 12, h: 16)
        return tw+ptw+10+25
    }
    
    func setupUI() {
        self.wantsLayer = true
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
            make.height.equalTo(16)
        }
        
        
       
        self.addSubview(proTitle)
        proTitle.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalToSuperview()
            make.height.equalTo(16)
        }
        
        self.addSubview(lineV)
        lineV.snp.makeConstraints { (make) in
            make.right.equalTo(proTitle.snp.left).offset(-5)
            make.centerY.equalToSuperview()
            make.height.equalToSuperview().offset(-12)
            make.width.equalTo(0.6)
        }
        
        self.zh_addBorder(radius: 12, borderColor: color_pay, borderWidth: 0.5)
    }
    
    func reloadTime(oldtime:String) {
        let time = Int(oldtime) ?? 0
        let color = time > 0 ? color_mainGreen:color_pay
        let titleT = time > 0 ? "剩余 ".localized()+"\(oldtime)"+" 天".localized():"基础版".localized()
        let proTT = time > 0 ? "续费".localized():"升级".localized()
        
        self.title.stringValue = titleT
        self.proTitle.stringValue = proTT
       
        self.title.textColor = color
        self.proTitle.textColor = color
        self.lineV.layer?.backgroundColor = color.cgColor
        self.zh_addBorder(radius: 12, borderColor: color, borderWidth: 0.5)
        
        self.snp.updateConstraints { (make) in
            make.width.equalTo(self.height(t: titleT, pt: proTT))
        }
    }
    
    lazy var lineV: NSView = {
        let lineV = NSView()
        lineV.wantsLayer = true
        lineV.layer?.backgroundColor = color_pay
            .withAlphaComponent(1).cgColor
        return lineV
    }()
    
    lazy var title: NSTextField = {
        let lab = NSTextField.label(title: "剩余 0 天", textColor: color_pay, font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    lazy var proTitle: NSTextField = {
        let lab = NSTextField.label(title: "升级", textColor: color_pay, font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    
}
